<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/4/22  16:46
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Model\BookModel;
use Admin\Common\Page;

class SearchCardController extends CashierBaseController {


    function index() {
        $keywords = I('get.keywords');
        $mapstr='';
        if (!empty($keywords)) {
            $mapstr='( card_id="'.$keywords.'" 
             or child_name like "%'.$keywords.'%" 
             or father_name like "%'.$keywords.'%" 
             or mother_name like "%'.$keywords.'%"
             or b.nickname like "%'.$keywords.'%" )';
        }
        $valid=I('get.valid');
        switch($valid){
            case "0":break;
            case "1":
                //有效
                if($mapstr==''){
                    $mapstr.='(expire_time >"'.date('Y-m-d H:i:s').'")';
                }else{
                    $mapstr.=' AND (expire_time >"'.date('Y-m-d H:i:s').'")';
                }
                break;
            case "2":
                //无效会员
                if($mapstr==''){
                    $mapstr.='(expire_time <"'.date('Y-m-d H:i:s').'")';
                }else{
                    $mapstr.=' AND (expire_time <"'.date('Y-m-d H:i:s').'")';
                }
                break;
            default:break;
        }
        $Card = M('Card');
        $count = $Card->alias('a')->where($mapstr)->join('user b on a.user_id=b.user_id','left')->count();// 查询满足要求的总记录数
        $Page = new  Page($count , C('page.size'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $this->assign('page' , $show);// 赋值分页输出
        $data = $Card->alias('a')->where($mapstr)->join('user b on a.user_id=b.user_id','left')->order('b.create_time desc')->limit($Page->firstRow . ',' . $Page->listRows)->order('a.card_id')->select();
        $this->data = $data;



        $this->display();
    }

    //用户借阅记录
    function userRent(){
        $card_id=I("post.card_id");
        $page=I("post.page")==""?1:I("post.page");
        $is_backed=I("post.is_backed")==""?2:I("post.is_backed");
        // 选择了卡号
        if ($card_id) {
            $dc=M('Card')->where(['card_id'=>$card_id])->find();
            if(!$dc['user_id']){
                $j=[
                    "data"=>[],
                    "total"=>1,
                    "page"=>1
                ];
                echo json_encode($j);
                return;
            }
            $user_id=$dc['user_id'];
            $map=['user_id'=>$user_id,'type'=>1,'state_order'=>['gt','0']];
        } else {
            // todo 没有选择则查出所有符合
            $valid = I('valid');
            if ($valid) {
                $cardMap = $valid == 1 ? 'GT' : 'LT';
                $searchMap['expire_time'] = [$cardMap, date('Y-m-d H:i:s')];
                $user_ids = M('card')->where($searchMap)->getField('user_id', true);
                $map['user_id'] = ['IN', $user_ids];
            }
        }
        if($is_backed=='1' || $is_backed=='0'){
            $map['is_backed']=$is_backed;
        }

        $interval_time = I('post.interval_time');
        if (!empty($interval_time)) {
            $time=explode('~',$interval_time);
            $time[0]=$time[0].' 00:00:00';
            $time[1]=$time[1].' 23:59:59';
            $map['create_time'] = [
                ['gt',$time[0]],
                ['lt',$time[1]]
            ];
        }
        $c=M("View_order_has_book")->where($map)->count();
        $total=ceil($c/C('page.size'));
        $d=M("View_order_has_book")->where($map)->limit(($page-1)*C('page.size').",".C('page.size'))->select();
        $j=[
            "data"=>$d,
            "total"=>$total,
            "page"=>$page
        ];
        $this->ajaxReturn($j);
    }

    


    function export(){
        $card_id=I('get.card_id');
        $is_backed=I('get.is_backed')==''?2:I('get.is_backed');

        // todo
        if ($card_id) {
            $dc=M('Card')->where(['card_id'=>$card_id])->find();
            if(!$dc['user_id']){
                $this->error('此会员不存在');
            }
            $user_id=$dc['user_id'];
            $map=['a.user_id'=>$user_id,'type'=>1];
        } else {
            // todo 没有选择则查出所有符合
            $valid = I('valid');
            if ($valid) {
                $cardMap = $valid == 1 ? 'GT' : 'LT';
                $searchMap['expire_time'] = [$cardMap, date('Y-m-d H:i:s')];
                $user_ids = M('card')->where($searchMap)->getField('user_id', true);
                $map['a.user_id'] = ['IN', $user_ids];
                $map['a.type'] = 1;
            }
        }

        if($is_backed=='1' || $is_backed=='0'){
            $map['a.is_backed']=$is_backed;
        }

        $interval_time = I('get.interval_time');
        if (!empty($interval_time)) {
            $time=explode('~',$interval_time);
            $time[0]=$time[0].' 00:00:00';
            $time[1]=$time[1].' 23:59:59';
            $map['a.create_time'] = [
                ['gt',$time[0]],
                ['lt',$time[1]]
            ];
        }
        $d=M("View_order_has_book")->alias('a')->join('card b on a.user_id=b.user_id','left')->where($map)->field('a.order_has_book_id as 记录ID,a.order_id,a.quantity,a.type,a.is_backed,a.is_apprised,a.book_id,a.tm,a.spbs,a.sm,a.zyz,a.dj,a.bb,a.yxxl,a.yc,a.bc,a.kb,a.sort,a.press,a.num_backed,a.num_broken,a.create_time,a.state_order,b.card_id as 会员卡')->select();
        $file=excel_export($d);
        ouput_Browser($file);
    }
}